package org.dreamwork.cai.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 12-9-4
 * Time: 上午12:41
 */
public class JDKProxyHandler implements InvocationHandler {
    private final Object target;

    public JDKProxyHandler (Object target) {
        this.target = target;
    }

    public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> type = target.getClass ();
        String name = method.getName ();
        Class<?>[] types = method.getParameterTypes ();
        try {
            Method m = type.getDeclaredMethod (name, types);
            return m.invoke (target, args);
        } catch (NoSuchMethodException nme) {
            // ignore
        }
        return method.invoke (target, args);
    }
}
